// MegaListBox.cpp : implementation file
//

#include "stdafx.h"
#include "MegaListBox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMegaListBox
// colors
#define COLOR_BACK			RGB(188, 188, 201)
#define COLOR_SEL			RGB(255, 255, 255)
#define COLOR_TEXT_SEL		RGB(0, 0, 0)
#define COLOR_TEXT			RGB(102, 102, 102)


CMegaListBox::CMegaListBox()
{
	m_pSelectedIcon = m_pUnselectedIcon = NULL;
	m_crBack = COLOR_BACK;
	m_crBack2 = COLOR_BACK;
	m_crText = COLOR_TEXT;
	m_crSel = COLOR_SEL;
	m_crSelText = COLOR_TEXT_SEL;
	m_bBack2 = FALSE;
}

CMegaListBox::~CMegaListBox()
{
	if(m_pSelectedIcon) delete m_pSelectedIcon;
	if(m_pUnselectedIcon) delete m_pUnselectedIcon;
}

void CMegaListBox::SetBitmaps(UINT uiSelected, UINT uiUnselected)
{
	if(m_pSelectedIcon) delete m_pSelectedIcon;
	if(m_pUnselectedIcon) delete m_pUnselectedIcon;

	m_pSelectedIcon = new CMegaBitmap();
	m_pUnselectedIcon = new CMegaBitmap();
	m_pSelectedIcon->LoadBitmap(uiSelected);
	m_pUnselectedIcon->LoadBitmap(uiUnselected);
}

void CMegaListBox::SetBackColor(COLORREF crBack)
{
	m_crBack = crBack;
	RedrawWindow();
}

void CMegaListBox::SetBackColor2(COLORREF crBack2)
{
	m_bBack2 = TRUE;
	m_crBack2 = crBack2;
	RedrawWindow();
}

void CMegaListBox::SetColors(COLORREF crBack, COLORREF crText, COLORREF crSel, COLORREF crSelText)
{
	m_crBack = crBack;
	m_crText = crText;
	m_crSel = crSel;
	m_crSelText = crSelText;
	if( !m_bBack2 ) m_crBack2 = crBack;
}

BEGIN_MESSAGE_MAP(CMegaListBox, CListBox)
	//{{AFX_MSG_MAP(CMegaListBox)
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMegaListBox message handlers

void CMegaListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your code to draw the specified item
	if(lpDrawItemStruct->CtlType != ODT_LISTBOX)
		return ;
	if((int) lpDrawItemStruct->itemID < 0)
		return ;

	CString str;
	GetText(lpDrawItemStruct->itemID, str);
	CDC dc;

	dc.Attach(lpDrawItemStruct->hDC);

	// Save these value to restore them when done drawing.
	COLORREF crOldTextColor = dc.GetTextColor();
	COLORREF crOldBkColor = dc.GetBkColor();

	CRect rcItem(lpDrawItemStruct->rcItem);
	CSize sz = m_pSelectedIcon?m_pSelectedIcon->GetBitmapDimension():0;

	// If this item is selected, set the background color 
	// and the text color to appropriate values. Also, erase
	// rect by filling it with the background color.
	if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
		(lpDrawItemStruct->itemState & ODS_SELECTED))
	{
		dc.SetTextColor(m_crSelText);
		dc.SetBkColor(m_crSel);
		dc.FillSolidRect(&rcItem, m_crSel);
		if(m_pSelectedIcon)
			m_pSelectedIcon->Draw(&dc, rcItem.left+2, rcItem.top+(rcItem.Height()-sz.cy)/2);
	}
	else
	{
		dc.SetTextColor(m_crText);
		dc.SetBkColor(m_crBack);
		COLORREF cr = m_crBack;
		if( m_bBack2 )
			if(((int)lpDrawItemStruct->itemID % 2) != 0) cr = m_crBack2;
		dc.FillSolidRect(&rcItem, cr);
		if(m_pUnselectedIcon)
			m_pUnselectedIcon->Draw(&dc, rcItem.left+2, rcItem.top+(rcItem.Height()-sz.cy)/2);
	}

	// If this item has the focus, draw a red frame around the
	// item's rect.
	if ((lpDrawItemStruct->itemAction | ODA_FOCUS) && (lpDrawItemStruct->itemState & ODS_FOCUS))
	{
		dc.DrawFocusRect(&rcItem);
	}
	else
	{
		if( !m_bBack2 )
		{
			CBrush br(m_crText);
			dc.FrameRect(&rcItem, &br);
		}
	}

	// Draw the text.
	rcItem.left += (sz.cx+4);
	rcItem.top++;
	rcItem.bottom--;
	dc.DrawText(
		str,
		&rcItem,
		DT_LEFT|DT_SINGLELINE|DT_VCENTER);

	// Reset the background color and the text color back to their
	// original values.
	dc.SetTextColor(crOldTextColor);
	dc.SetBkColor(crOldBkColor);

	dc.Detach();
}

void CMegaListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
	// TODO: Add your code to determine the size of specified item
	CRect rc;
	GetClientRect(&rc);
	CSize sz = m_pSelectedIcon->GetBitmapDimension();
	lpMeasureItemStruct->itemHeight = sz.cy + 2;
	lpMeasureItemStruct->itemWidth = rc.Width();
}

int CMegaListBox::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct) 
{
	// TODO: Add your code to determine the sorting order of the specified items
	// return -1 = item 1 sorts before item 2
	// return 0 = item 1 and item 2 sort the same
	// return 1 = item 1 sorts after item 2
	
	return 0;
}

BOOL CMegaListBox::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	CRect rc;
	GetClientRect(&rc);
	pDC->FillSolidRect(&rc, m_crBack);

	return TRUE;
}

